package controllers

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"main.go/database"
	"main.go/model"
	"net/http"
	"strconv"
)

//func Login_goods_company(c *gin.Context) {
//	fmt.Println("Login_goods_company")
//	db := database.GetDB()
//	var goods_company model.Goods_company
//	request := make(map[string]interface{})
//	c.ShouldBind(&request)
//	fmt.Println(request)
//	goods_company.Goods_company_name = request["goods_company_name"].(string)
//	goods_company.Goods_company_password = request["goods_company_password"].(string)
//	fmt.Println("port_company_name为")
//	fmt.Println(goods_company.Goods_company_name)
//	fmt.Println("密码为")
//	fmt.Println(goods_company.Goods_company_password)
//	if Find_goods_company_password(db, goods_company) {
//		fmt.Println("登录成功")
//		c.JSON(200, gin.H{
//			"code": "200",
//			"msg":  "ok",
//		})
//	} else {
//		c.JSON(404, gin.H{
//			"code": "404",
//			"msg":  "no",
//		})
//		fmt.Println("登录失败")
//	}
//}
//func Find_goods_company_password(db *gorm.DB, test model.Goods_company) bool {
//	var goods_company model.Goods_company
//	fmt.Println("Find_port_company_password")
//	fmt.Println(test.Goods_company_name)
//	fmt.Println(test.Goods_company_password)
//	db.Where("goods_company_name = ? AND goods_company_password = ?", test.Goods_company_name, test.Goods_company_password).First(&goods_company)
//	fmt.Println("Find_port_company_password2")
//	if goods_company.Goods_company_name != "" {
//		fmt.Println(goods_company.Goods_company_name)
//		return true
//	}
//	return false
//}

//查找
func Find_goods_company(db *gorm.DB, request model.Goods_company) bool {
	var goods_company model.Goods_company
	db.Where("goods_company_name = ?", request.Goods_company_name).First(&goods_company)
	if goods_company.Goods_company_name != "" {
		fmt.Println("查找成功")
		return true
	}
	fmt.Println("查找失败")
	return false
}

//插入
func Insert_goods_company(db *gorm.DB, request model.Goods_company) bool {
	if Find_goods_company(db, request) == true {
		fmt.Println("该货代公司已存在，不能插入")
		return false
	}
	db.Create(&request) //创建一个元组
	fmt.Println("插入成功")
	return true
}

//删除
func Delete_goods_company(db *gorm.DB, request model.Goods_company) bool {
	if Find_goods_company(db, request) == false {
		fmt.Println("不存在该货代公司，无法删除")
		return false
	}
	db.Where("goods_company_name = ?", request.Goods_company_name).Delete(&request)
	fmt.Println("删除成功")
	return true
}

//更新
func Update_goods_company(db *gorm.DB, request model.Goods_company) bool {
	if Find_goods_company(db, request) == false {
		fmt.Println("无该货代公司，无法更新")
		return false
	}
	db.Model(&request).Where("goods_company_name = ?", request.Goods_company_name).Updates(
		model.Goods_company{
			Goods_company_name:   request.Goods_company_name,
			Goods_company_charge: request.Goods_company_charge,
		},
	)
	fmt.Println("更新成功")
	return true
}

func Web_show_goods_company(c *gin.Context) {
	db := database.GetDB()
	var goods_company []model.Goods_company
	result := db.Table("goods_companies").Find(&goods_company)
	if result != nil {
		fmt.Println("!!")
	}
	fmt.Println(goods_company)
	c.JSON(http.StatusOK, gin.H{
		"code":          "200",
		"goods_company": goods_company,
		"msg":           "成功",
	})
}
func Web_get_goods_company(c *gin.Context) {
	db := database.GetDB()
	var goods_company1 model.Goods_company
	var goods_company2 []model.Goods_company
	request := make(map[string]interface{})
	c.ShouldBind(&request)
	fmt.Println(request)
	goods_company1.Goods_company_name = request["goods_company_name"].(string)
	db.Where("goods_company_name = ?", goods_company1.Goods_company_name).First(&goods_company2)
	if goods_company2[0].Goods_company_name != "" {
		fmt.Println("成功查找到代货公司")
		c.JSON(200, gin.H{
			"code":          "200",
			"goods_company": goods_company2, //传一个对象
			"msg":           "成功",
		})
	} else {
		c.JSON(404, gin.H{
			"code":   "404",
			"car_id": goods_company2, //传一个空对象
			"msg":    "no",
		})
		fmt.Println("未找到该代货公司")
	}
}
func Web_add_goods_company(c *gin.Context) {
	db := database.GetDB()
	var goods_company model.Goods_company
	request := make(map[string]interface{})
	c.ShouldBind(&request)
	fmt.Println(request)
	goods_company.Goods_company_name = request["goods_company_name"].(string)
	goods_company.Goods_company_charge, _ = strconv.Atoi(request["goods_company_charge"].(string))
	if Insert_goods_company(db, goods_company) {
		c.JSON(200, gin.H{
			"code": "200",
			"msg":  "成功",
		})
		fmt.Println("添加成功")
	} else {
		c.JSON(404, gin.H{
			"code": "404",
			"msg":  "添加失败",
		})
		fmt.Println("添加失败")
	}
}
func Web_delete_goods_company(c *gin.Context) {
	db := database.GetDB()
	var goods_company model.Goods_company
	request := make(map[string]interface{})
	c.ShouldBind(&request)
	fmt.Println(request)
	goods_company.Goods_company_name = request["goods_company_name"].(string)
	if Delete_goods_company(db, goods_company) {
		c.JSON(200, gin.H{
			"code": "200",
			"msg":  "删除成功",
		})
		fmt.Println("删除成功")
	} else {
		c.JSON(404, gin.H{
			"code": "404",
			"msg":  "删除失败",
		})
		fmt.Println("删除失败")
	}
}
func Web_update_goods_company(c *gin.Context) {
	db := database.GetDB()
	var goods_company model.Goods_company
	request := make(map[string]interface{})
	c.ShouldBind(&request)
	fmt.Println(request)
	goods_company.Goods_company_name = request["goods_company_name"].(string)
	goods_company.Goods_company_charge, _ = strconv.Atoi(request["goods_company_charge"].(string))
	if Update_goods_company(db, goods_company) {
		c.JSON(200, gin.H{
			"code": "200",
			"msg":  "成功",
		})
		fmt.Println("添加成功")
	} else {
		c.JSON(404, gin.H{
			"code": "404",
			"msg":  "添加失败",
		})
		fmt.Println("添加失败")
	}
}
